package unit10;

import java.io.*;

public class Demo02 {
    //使用java的输入和输出流将javaProjectAllWork/src/unit10/log.txt目录中的log.txt文件按行读出，每读出一行就顺序添加行号，并写入到javaProjectAllWork/src/unit10/log2.txt目录中的log2.txt文件中
    public static void main(String[] args) {
        String inputFilePath = "javaProjectAllWork/src/unit10/log.txt";
        String outputFilePath = "javaProjectAllWork/src/unit10/log2.txt";

        try (BufferedReader reader = new BufferedReader(new FileReader(inputFilePath));
             BufferedWriter writer = new BufferedWriter(new FileWriter(outputFilePath))) {

            String line;
            int lineNumber = 1;

            while ((line = reader.readLine()) != null) {
                String lineWithNumber = lineNumber + ": " + line;
                writer.write(lineWithNumber);
                writer.newLine();
                lineNumber++;
            }

            System.out.println("File processed successfully.");

        } catch (IOException e) {
            System.out.println("An error occurred: " + e.getMessage());
        }
    }
}
